<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { axiosrnewmedical } from '@/http/axiosRead';

interface GuideDetail {
  article: {
    id: number;
    catId: number;
    categoryName: string;
    title: string;
    covering: string;
    viewCnt: number;
    createdAt: string;
  };
  contentHtml: string;
}

const router = useRouter();
const route = useRoute();
const articleDetail = ref<GuideDetail | null>(null);
const loading = ref(false);
const articleId = ref(Number(route.params.articleId));

// 获取文章详情
const getGuideDetail = async () => {
  try {
    loading.value = true;
    const response = await axiosrnewmedical.get(`/Guide/detail/${articleId.value}`);
    
    if (response.data.code === 200) {
      articleDetail.value = response.data.data;
    }
  } catch (error) {
    console.error('获取就医指南文章详情失败:', error);
  } finally {
    loading.value = false;
  }
};

// 返回文章列表
const goBack = () => {
  router.go(-1);
};

// 格式化日期
const formatDate = (dateStr: string) => {
  const date = new Date(dateStr);
  return date.toLocaleDateString('zh-CN', {
    year: 'numeric',
    month: 'long',
    day: 'numeric'
  });
};

onMounted(() => {
  getGuideDetail();
});
</script>

<template>
  <div class="guide-detail-container">
    <!-- 导航栏 -->
    <van-nav-bar 
      title="就医指南详情" 
      left-arrow 
      @click-left="goBack" 
      fixed />
    
    <!-- 文章详情 -->
    <div class="article-detail">
      <van-loading v-if="loading" class="loading" size="24px">加载中...</van-loading>
      
      <van-empty v-else-if="!articleDetail" description="文章不存在" />
      
      <div v-else class="detail-content">
        <!-- 文章头部信息 -->
        <div class="article-header">
          <h1 class="article-title">{{ articleDetail.article.title }}</h1>
          
          <div class="article-meta">
            <span class="meta-item">
              <van-icon name="bookmark-o" />
              {{ articleDetail.article.categoryName }}
            </span>
            <span class="meta-item">
              <van-icon name="eye-o" />
              {{ articleDetail.article.viewCnt || 0 }}次阅读
            </span>
            <span class="meta-item">
              <van-icon name="clock-o" />
              {{ formatDate(articleDetail.article.createdAt) }}
            </span>
          </div>
        </div>
        
        <!-- 分割线 -->
        <van-divider />
        
        <!-- 文章内容 -->
        <div class="article-content" v-html="articleDetail.contentHtml"></div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.guide-detail-container {
  background-color: #fff;
  min-height: 100vh;
  padding-top: 46px;
}

.article-detail {
  min-height: calc(100vh - 46px);
}

.loading {
  display: flex;
  justify-content: center;
  padding: 40px 0;
}

.detail-content {
  padding: 0;
}

.article-header {
  padding: 20px 16px 16px 16px;
  background-color: #fff;
}

.article-title {
  font-size: 20px;
  font-weight: bold;
  color: #333;
  line-height: 1.4;
  margin: 0 0 16px 0;
}

.article-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  font-size: 13px;
  color: #666;
}

.meta-item {
  display: flex;
  align-items: center;
  gap: 4px;
}

.meta-item .van-icon {
  font-size: 13px;
  color: #999;
}

.article-content {
  padding: 0 16px 20px 16px;
  line-height: 1.6;
  color: #333;
}

/* 文章内容样式 */
.article-content :deep(h1),
.article-content :deep(h2),
.article-content :deep(h3),
.article-content :deep(h4),
.article-content :deep(h5),
.article-content :deep(h6) {
  color: #333;
  margin: 20px 0 12px 0;
  font-weight: bold;
}

.article-content :deep(p) {
  margin: 12px 0;
  text-align: justify;
}

.article-content :deep(ul),
.article-content :deep(ol) {
  margin: 12px 0;
  padding-left: 20px;
}

.article-content :deep(li) {
  margin: 6px 0;
}

.article-content :deep(img) {
  max-width: 100%;
  height: auto;
  border-radius: 8px;
  margin: 12px 0;
}

.article-content :deep(blockquote) {
  background-color: #f8f9fa;
  border-left: 4px solid #338aff;
  padding: 12px 16px;
  margin: 16px 0;
  border-radius: 0 8px 8px 0;
}

.article-content :deep(code) {
  background-color: #f8f9fa;
  padding: 2px 6px;
  border-radius: 4px;
  font-family: 'Courier New', monospace;
  font-size: 14px;
}

.article-content :deep(pre) {
  background-color: #f8f9fa;
  padding: 16px;
  border-radius: 8px;
  overflow-x: auto;
  margin: 16px 0;
}

.article-content :deep(table) {
  width: 100%;
  border-collapse: collapse;
  margin: 16px 0;
}

.article-content :deep(th),
.article-content :deep(td) {
  border: 1px solid #e8e8e8;
  padding: 8px 12px;
  text-align: left;
}

.article-content :deep(th) {
  background-color: #f8f9fa;
  font-weight: bold;
}
</style> 